﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Data.SQLite;
using MedicLibrary.Controler;

namespace MedicLibrary.Model
{


    class HangSX
    {
        public int ID { get; set; }
        public String MaHangSX { get; set; }
        public String TenHangSX { get; set; }
        public String QuocGia { get; set; }
        public String DiaChi { get; set; }
        public String GhiChu { get; set; }

        public HangSX()
        {
            this.ID = 0;
            this.MaHangSX = "";
            this.TenHangSX = "";
            this.QuocGia = "";
            this.DiaChi = "";
            this.GhiChu = "";
        }

        public static DataTable getDanhSachHangSX( String para )
        {
            DBConnector dbc = new DBConnector();
            String sql = "";
            if (para != "")
            {
                sql = "SELECT * FROM HangSX WHERE TenHangSX LIKE '%" + para + "%' OR QuocGia LIKE '%" + para + "%' LIMIT 100";
            }
            else
                sql = "SELECT * FROM HangSX LIMIT 100";
            return dbc.GetDataTable(sql);
        }

        public static DataTable getThongTinHangSX(int ID)
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT * FROM HangSX ";
            sql += " WHERE ID =" + ID;
            return dbc.GetDataTable(sql);

        }

        public bool readThongTinHangSX(int ID)
        {
            DataTable table = getThongTinHangSX(ID);
            if (table.Rows.Count == 0)
                return false;
            foreach (DataRow r in table.Rows)
            {
                this.ID = Int32.Parse(r["ID"].ToString());
                this.TenHangSX = r["TenHangSX"].ToString();
                this.MaHangSX = r["MaHangSX"].ToString();
                this.QuocGia = r["QuocGia"].ToString();
                this.DiaChi = r["DiaChi"].ToString();
                this.GhiChu = r["GhiChu"].ToString();
            }
            return true;
        }

        public bool insertHangSX()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("ID", this.ID.ToString());
            data.Add("MaHangSX", "HSX" + Session.generateID(4, this.ID));
            data.Add("TenHangSX", this.TenHangSX);
            data.Add("QuocGia", this.QuocGia);
            data.Add("DiaChi", this.DiaChi);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Insert("HangSX", data);
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Thêm Hãng thành công!");
            return true;

        }

        public bool updateHangSX()
        {
            DBConnector dbc = new DBConnector();
            Dictionary<String, String> data = new Dictionary<String, String>();
            data.Add("MaHangSX", this.MaHangSX);
            data.Add("TenHangSX", this.TenHangSX);
            data.Add("QuocGia", this.QuocGia);
            data.Add("DiaChi", this.DiaChi);
            data.Add("GhiChu", this.GhiChu);
            try
            {
                dbc.Update("HangSX", data, String.Format("HangSX.ID = {0}", this.ID));
            }
            catch (Exception crap)
            {
                MessageBox.Show(crap.Message);
                return false;
            }
            MessageBox.Show("Cập nhật Hãng thành công!");
            return true;
        }

        public bool deleteHangSX()
        {
            if (MessageBox.Show("Bạn thật sự muốn xóa Hãng này ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
            {
                DBConnector dbc = new DBConnector();
                return dbc.Delete("HangSX", String.Format("ID = {0}", this.ID));
            }
            else
                return false;
        }

        public void getMaxID()
        {
            DBConnector dbc = new DBConnector();
            String sql = "SELECT MAX(ID) as MAXID FROM HangSX";
            this.ID = Int32.Parse(dbc.GetDataTable(sql).Rows[0]["MAXID"].ToString()) + 1;
        }
    }
}
